-
Notifications
You must be signed in to change notification settings - Fork 84
(feat) O3-4201: Enhance Number Question Labels Display Unit and Range (Min/Max) from Concept #454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…(min-max) to label and min/max to form
@samuelmale @ibacher please let me know if you had other approaches in mind for this or issues |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add some test coverage asserting that the units and labels are incorporated in the label?
src/hooks/useFormFieldsMeta.ts
Outdated
{ | ||
if(matchingConcept.units) | ||
{ | ||
field.label = field.label + " (" + matchingConcept.units + ")"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This potentially breaks i18n workflows because it mutates the field's label which is implicitly used as the translation key. Since the concept is stored under the field's meta, this can safely be done within the number component post label translation; something like:
<NumberInput
id={field.id}
label={<FieldLabel field={field} customLabel={t(field.label) + extractFieldUnitsAndRange(field.meta.concept)} />}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@samuelmale thanks, moved the units and range to the number component. Also for a concept that defines min but not max it looks like
leaving critical alone for now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should harmonize the view here to what we're doing here, which is >= 0
or <= 100
things like that. (Note that "Min" is hard to support because now that's a string that needs to be translated).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, that was always going to be a possibility... This just made it apparent for that one case.
@@ -5,7 +5,7 @@ import { type FetchResponse, type OpenmrsResource, openmrsFetch, restBaseUrl } f | |||
type ConceptFetchResponse = FetchResponse<{ results: Array<OpenmrsResource> }>; | |||
|
|||
const conceptRepresentation = | |||
'custom:(uuid,display,conceptClass:(uuid,display),answers:(uuid,display),conceptMappings:(conceptReferenceTerm:(conceptSource:(name),code)))'; | |||
'custom:(units,lowAbsolute,hiAbsolute,uuid,display,conceptClass:(uuid,display),answers:(uuid,display),conceptMappings:(conceptReferenceTerm:(conceptSource:(name),code)))'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ibacher when is it necessary to use hiCritical
vs lowCritical
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of the values that aren't lowAbsolute
and hiAbsolute
are basically only for displaying concerning results (outside the normal range, but inside the critical range) or critical results (outside the critical range, but within the absolute range). Does that make sense? lowAbsolute
and hiAbsolute
should be rare and only in cases where the value can never exceed that range (e.g., SpO2 is a percent, so it's always between 0 and 100; heights and weights should never be a negative number, things like that).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we can handle critical icons in a separate PR.
…l, add test concept with units and range
@@ -11,6 +11,28 @@ import { useFormProviderContext } from '../../../provider/form-provider'; | |||
import FieldLabel from '../../field-label/field-label.component'; | |||
import { isEmpty } from '../../../validators/form-validator'; | |||
|
|||
|
|||
const extractFieldUnitsAndRange = (concept) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you wrap this in a useCallback?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, actually, you shouldn't do that here. This is actually done correctly. Functions defined inside functional components would be swapped with a new instance every time the function is called (so, every time the renderer detects that the component might change). useCallback()
prevents that by ensuring the variable keeps a stable reference to the same function until the dependency array changes. Since this function isn't defined in a functional component body, references to this function will always be stable references to the same function, so useCallback()
isn't needed here (and, IMO, we should prefer these ancillary functions be defined outside of functional components wherever feasible).
All that said, the concept
argument here must have a type defined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oohh yeah! My bad! For some reason I assumed that the function was defined in the body of the functional component which isn't the case here. It definitely doesn't make sense to memoize such a function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should concept come from esm-framework? Or somewhere else or just manually define the interface there? Thanks
@@ -11,6 +11,28 @@ import { useFormProviderContext } from '../../../provider/form-provider'; | |||
import FieldLabel from '../../field-label/field-label.component'; | |||
import { isEmpty } from '../../../validators/form-validator'; | |||
|
|||
|
|||
const extractFieldUnitsAndRange = (concept) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, actually, you shouldn't do that here. This is actually done correctly. Functions defined inside functional components would be swapped with a new instance every time the function is called (so, every time the renderer detects that the component might change). useCallback()
prevents that by ensuring the variable keeps a stable reference to the same function until the dependency array changes. Since this function isn't defined in a functional component body, references to this function will always be stable references to the same function, so useCallback()
isn't needed here (and, IMO, we should prefer these ancillary functions be defined outside of functional components wherever feasible).
All that said, the concept
argument here must have a type defined.
@@ -61,7 +83,7 @@ const NumberField: React.FC<FormFieldInputProps> = ({ field, value, errors, warn | |||
id={field.id} | |||
invalid={errors.length > 0} | |||
invalidText={errors[0]?.message} | |||
label={<FieldLabel field={field} />} | |||
label={<FieldLabel field={field} customLabel={t(field.label) + extractFieldUnitsAndRange(field.meta?.concept)} />} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't do string concatenation like this with translated strings. Use i18next's interpolation feature for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, the code now hard-codes a word order that doesn't make sense in LTR languages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the label to t('{{fieldDescription}} {{unitsAndRange}}' but not sure if that's correct as I haven't used i18n before or tested a right to left language case. Thanks for the i18n help.
@@ -22,6 +22,25 @@ const numberFieldMock = { | |||
readonly: false, | |||
}; | |||
|
|||
const numberFieldMockWithUnitsAndRange = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to have a more comprehensive set of tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added a few more cases (only units, only range, only max)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add one for lowAbsolute only as well? That should cover things.
const hasUpperLimit = hiAbsolute != null; | ||
|
||
if (hasLowerLimit && hasUpperLimit) { | ||
return ` (${lowAbsolute} - ${hiAbsolute} ${displayUnit})`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't hard-code prepended spaces in strings here.
…ber-Question-Labels-Display-Unit-and-Range-(Min/Max)-from-Concept
…abel i18n, unit/range test cases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not really sure about jest.mock('react-i18next', () => ({...
it seemed needed for the new i18n label t('{{fieldDescription}} {{unitsAndRange}}'... but I'm not sure if that's correct
src/hooks/useFormFieldsMeta.ts
Outdated
@@ -11,6 +11,20 @@ export function useFormFieldsMeta(rawFormFields: FormField[], concepts: OpenmrsR | |||
const matchingConcept = findConceptByReference(field.questionOptions.concept, concepts); | |||
field.questionOptions.concept = matchingConcept ? matchingConcept.uuid : field.questionOptions.concept; | |||
field.label = field.label ? field.label : matchingConcept?.display; | |||
|
|||
if (matchingConcept) { | |||
if (matchingConcept.lowAbsolute != undefined && matchingConcept.hiAbsolute != undefined) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's always better to use typeof x !== 'undefined'
, though this kind of works, but only because SWC will re-write the undefined
s here to void 0
. In any case, please use exact comparisons (!==
and ===
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, please just implement @samuelmale's suggestion. It's much cleaner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
applied suggestion @samuelmale ty
const hasLowerLimit = lowAbsolute != null; | ||
const hasUpperLimit = hiAbsolute != null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again !==
. But I think here it's preferential to just use isNil()
from lodash.
@@ -10,6 +10,28 @@ import { useTranslation } from 'react-i18next'; | |||
import { useFormProviderContext } from '../../../provider/form-provider'; | |||
import FieldLabel from '../../field-label/field-label.component'; | |||
import { isEmpty } from '../../../validators/form-validator'; | |||
import { Concept } from '@openmrs/esm-framework'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see our extension documentation on how imports are to be ordered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved useTranslation up as well
{ | ||
fieldDescription: t(field.label), | ||
unitsAndRange: extractFieldUnitsAndRange(field.meta?.concept), | ||
interpolation: { escapeValue: false } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
without escapeValue: false, / doesn't show correctly, it becomes /
eg BMI (Kg/m2):
@@ -61,7 +83,12 @@ const NumberField: React.FC<FormFieldInputProps> = ({ field, value, errors, warn | |||
id={field.id} | |||
invalid={errors.length > 0} | |||
invalidText={errors[0]?.message} | |||
label={<FieldLabel field={field} />} | |||
label={<FieldLabel field={field} customLabel={t('{{fieldDescription}} {{unitsAndRange}}', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should have a real key here with this interpolation as the defaultValue
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks again for i18n help, I changed the test too to t: (key, defaultValueOrOptions, options), hopefully that's correct now that there's a key and default value but not sure
useTranslation: () => ({ | ||
t: (key, options) => { | ||
if (options && 'fieldDescription' in options) { | ||
return `${options.fieldDescription} ${options.unitsAndRange || ''}`.trim(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since extractFieldUnitsAndRange()
always returns a string:
return `${options.fieldDescription} ${options.unitsAndRange || ''}`.trim(); | |
return `${options.fieldDescription} ${options.unitsAndRange}`.trim(); |
@@ -22,6 +22,25 @@ const numberFieldMock = { | |||
readonly: false, | |||
}; | |||
|
|||
const numberFieldMockWithUnitsAndRange = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add one for lowAbsolute only as well? That should cover things.
…nly min test, cleaner hi/low from matchingConcept
Add (unit) (min-max) to label, validate input against concept min/max
https://openmrs.atlassian.net/browse/O3-4201
Related: https://openmrs.atlassian.net/browse/O3-4122
4122 also suggests a warning for values within the absolute range but outside normal or critical range. Looking for suggestions on how to implement that, and how to add a test case for a concept with units and min/max
Requirements
Summary
Screenshots
Related Issue
Other